package com.demo2do.fms.admin.entity.query;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 * 所属交通工具的辅助查询
 * @author Wy
 *
 */
public class VehicleQuery implements QueryBuilder{
    
     private List<Long> companyIds;
     
     /** 驳运车代码*/
     private String code;
      
     /** 驳运车名称*/
     private String name;
      
     /** 驳运车车牌*/
     private String vehicleBarCode;
     
     private QueryOrder order;
        
     private QueryFilter filter;
     
     /**
      * 
      */
     public VehicleQuery(){
         order = new QueryOrder();
     }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {

           filter = new QueryFilter().addLike("code", code)
                                      .addLike("name", name)
                                      .addLike("barCode", vehicleBarCode)
                                      .addEquals("disabled", false);
			
			if(CollectionUtils.isNotEmpty(companyIds)) {
				
				filter.addSentence("organization.id IN (:companyIds)", "companyIds", companyIds);
				
			}
				
         }		
			
         return filter;
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
	public QueryOrder getOrder() {
		return order;
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
	public String buildSentenceTemplate() {
		String sentence = "FROM Vehicle WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + order.sentence();
		return sentence;
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
	public Map<String, Object> buildParameters() {
		return getPreparedFilter(false).parameters();
	}
	
	/**
     * 
     * @return
     */
	public QueryFilter getFilter(){
		return getPreparedFilter(false);
	}

	/**
	 * 
	 * @return
	 */
	public List<Long> getCompanyIds() {
        return companyIds;
    }

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the vehicleBarCode
	 */
	public String getVehicleBarCode() {
		return vehicleBarCode;
	}

	/**
	 * 
	 * @param companyIds
	 */
	public void setCompanyIds(List<Long> companyIds) {
        this.companyIds = companyIds;
    }

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = StringUtils.trim(code);
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = StringUtils.trim(name);
	}

	/**
	 * @param vehicleBarCode the vehicleBarCode to set
	 */
	public void setVehicleBarCode(String vehicleBarCode) {
		this.vehicleBarCode = StringUtils.trim(vehicleBarCode);
	}

}
